package com.hx.order.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hx.common.annotation.Log;
import com.hx.common.core.controller.BaseController;
import com.hx.common.core.domain.AjaxResult;
import com.hx.common.enums.BusinessType;
import com.hx.order.domain.CartItem;
import com.hx.order.service.ICartItemService;
import com.hx.common.utils.poi.ExcelUtil;
import com.hx.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 购物车Controller
 *
 * @author ord
 * @date 2023-05-28
 */
@RestController
@RequestMapping("/cart")
public class CartItemController extends BaseController {
    @Autowired
    private ICartItemService cartItemService;

    /**
     * 查询购物车列表
     */
    @PreAuthorize("@ss.hasPermi('order:cart:list')")
    @GetMapping("/list")
    public TableDataInfo list(CartItem cartItem)
    {
        startPage();
        List<CartItem> list = cartItemService.selectCartItemList(cartItem);
        return getDataTable(list);
    }

    /**
     * 导出购物车列表
     */
    @PreAuthorize("@ss.hasPermi('order:cart:export')")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CartItem cartItem)
    {
        List<CartItem> list = cartItemService.selectCartItemList(cartItem);
        ExcelUtil<CartItem> util = new ExcelUtil<CartItem>(CartItem.class);
        util.exportExcel(response, list, "购物车数据");
    }

    /**
     * 获取购物车详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:cart:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cartItemService.selectCartItemById(id));
    }

    /**
     * 新增购物车
     */
    @PreAuthorize("@ss.hasPermi('orders:cart:add')")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(CartItem cartItem, MultipartFile file) throws IOException {
        System.out.println(file);
        String path = "C:\\images\\";
        long size = file.getSize();
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String filename = file.getOriginalFilename();
        String type = filename.substring(filename.lastIndexOf("."));
        file.transferTo(new File(path + uuid + type));
        InputStream inputStream = new FileInputStream(new File(path + uuid + type));
        byte[] bytes = inputStream.readAllBytes();
        String md5 = DigestUtils.md5DigestAsHex(bytes);
        cartItem.setProductPic("http://localhost:80/images/"+uuid+type);
        return toAjax(cartItemService.insertCartItem(cartItem));
    }

    /**
     * 修改购物车
     */
    @PreAuthorize("@ss.hasPermi('order:cart:edit')")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CartItem cartItem)
    {
        return toAjax(cartItemService.updateCartItem(cartItem));
    }

    /**
     * 删除购物车
     */
    @PreAuthorize("@ss.hasPermi('order:cart:remove')")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cartItemService.deleteCartItemByIds(ids));
    }
}
